package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
	_ "github.com/zeromicro/zero-contrib/zrpc/registry/nacos"
	"go-zero-demo/common/global"
	"go-zero-demo/user-api/internal/config"
	"go-zero-demo/user-api/internal/handler"
	initialize2 "go-zero-demo/user-api/internal/initialize/nacos"
	"go-zero-demo/user-api/internal/initialize/viper"
	"go-zero-demo/user-api/internal/svc"
)

var configFile = flag.String("f", "user-api/etc/user-api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	//conf.MustLoad(*configFile, &c, conf.UseEnv()) 配置yaml文件可以使用${环境变量}
	conf.MustLoad(*configFile, &c)
	// SetUp 日志 Prometheus
	//rest.WithNotFoundHandler()
	//server := rest.MustNewServer(c.RestConf, rest.WithNotFoundHandler())
	server := rest.MustNewServer(c.RestConf)
	//server := rest.MustNewServer(c.RestConf, func(server *rest.Server) {
	//	server.AddRoutes()
	//})
	defer server.Stop()
	// register nacos
	// go routine 安全的 threading.GoSafe()
	initialize2.RegisterNacos(c)
	// init config center
	viper.InitViperConfig()
	// 禁止掉答应cpu
	//logx.DisableStat()

	server.Use(global.NewSetUidToCtxMiddleware().Handle)

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)
	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
